package com.taichuan.selfcheck.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import com.taichuan.keysupport.Key;
import com.taichuan.mvp.fragment.BaseFragmentManager;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.entity.TestResultEntity;
import com.taichuan.selfcheck.mvp.presenter.MainPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.MainInterface;
import com.taichuan.selfcheck.ui.activity.MainActivity;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.selfcheck.utils.HardwareUtils;
import com.taichuan.tcutils.taichuan.SystemUtils;
import com.taichuan.tcutils.taichuan.TcDeviceType;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class MainFragment2 extends AppBaseFragment<MainInterface, MainPresenter> implements MainInterface {

    @BindView(R.id.test_network_button)
    Button testNetworkBtn;
    @BindView(R.id.adb_test_button)
    Button adb_test_button;
    @BindView(R.id.video_test_button)
    Button video_test_button;
    @BindView(R.id.temp_test_button)
    Button temp_test_button;
    @BindView(R.id.check_result_button)
    Button check_result_button;
    @BindView(R.id.auto_check_button)
    Button auto_check_button;
    @BindView(R.id.double_camera_test_button)
    Button doubleCameraTestBtn;
    @BindView(R.id.blueToothTest)
    Button btnBleTest;
    @BindView(R.id.qrcode_test_button)
    Button qrcodeTestBtn;
    @BindView(R.id.mobile_network_test_button)
    Button mobileNetworkBtn;
    @BindView(R.id.finger_test_button)
    Button fingerTestBtn;

    private BaseFragmentManager activityBaseFragmentManager;
    private MainActivity mMainActivity;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_main_fragment2;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMainActivity = (MainActivity) context;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //隐藏MainActivity 中的返回按钮
//            showOrHideMainBottomButton(false);
        } else {
            //显示MainActivity 中的返回按钮
            showTitle(R.string.settings_function_test);
        }
    }

    @OnClick({
            R.id.test_network_button,
            R.id.adb_test_button,
            R.id.video_test_button,
            R.id.check_result_button,
            R.id.auto_check_button,
            R.id.temp_test_button,
            R.id.double_camera_test_button,
            R.id.blueToothTest,
            R.id.qrcode_test_button,
            R.id.mobile_network_test_button,
            R.id.finger_test_button
    })
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.test_network_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.network_test);
                    activityBaseFragmentManager.replace(new NetworkTestFragment());
                }
                break;
            }
            case R.id.adb_test_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.adb_test);
                    activityBaseFragmentManager.replace(new AdbControlFragment());
                }
                break;
            }
            case R.id.video_test_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.video_test);
                    activityBaseFragmentManager.replace(new VideoTestFragment());
                }
                break;
            }
            case R.id.temp_test_button: {
                if (SystemUtils.getTcDeviceType() == TcDeviceType.RK3399_VERTICAL_TOUCH_TEMP_DOOR ||
                SystemUtils.isVankeSiteBoxDevice()) {
                    showTitle(R.string.temp_test);
                    activityBaseFragmentManager.replace(new TempTestFragment());
                } else {
                    showMsg("该设备暂不支持温度测试");
                }
                break;
            }
            case R.id.double_camera_test_button: {
                if (SystemUtils.isRk3399()) {
                    int tittleId;
                    //初始化摄像头工具类
                    if (!SystemUtils.isVankeSiteBoxDevice()){           //常规设备
                        tittleId = R.string.double_camera_test;
                    }else {                                             //万科智慧工地项目设备
                        tittleId = R.string.third_camera_test;
                    }

                    showTitle(tittleId);
                    activityBaseFragmentManager.replace(new TestDoubleCameraFragment());
                } else {
                    showMsg("该设备暂不支持双目摄像头测试");
                }
                break;
            }
            case R.id.blueToothTest: {
                //蓝牙功能测试
                if (SystemUtils.getTcDeviceType() == TcDeviceType.A64_VERTICAL_TOUCH ||
                        SystemUtils.getTcDeviceType() == TcDeviceType.A64_VERTICAL_TOUCH_HZ01 ||
                        SystemUtils.getTcDeviceType() == TcDeviceType.A64_VERTICAL_KEY ||
                        SystemUtils.getTcDeviceType() == TcDeviceType.A64_LANDSCAPE_KEY ||
                        SystemUtils.getTcDeviceType() == TcDeviceType.A33_LANDSCAPE_KEY ||
                        SystemUtils.getTcDeviceType() == TcDeviceType.L10_Pro ||
                        SystemUtils.isRk3399()) {
                    showTitle(R.string.blue_tooth_test);
                    activityBaseFragmentManager.replace(new BlueToothTestFragment());
                } else {
                    showMsg("该设备暂不支持蓝牙功能测试");
                }
                break;
            }
            case R.id.check_result_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.check_result);
                    activityBaseFragmentManager.replace(new TestResultFragment());
                }
                break;
            }
            case R.id.auto_check_button: {
                EventBus.getDefault().post(new TestResultEntity(0, getResources().getString(R.string.auto_check), PropertyDefaultValue.FUNCTION_TEST_RESULT));
                break;
            }
            case R.id.qrcode_test_button: {
                // 二维码测试
                if (SystemUtils.isRk3399() && HardwareUtils.is20Devices()) {
                    showTitle(R.string.qrcode_test);
                    activityBaseFragmentManager.replace(new QrcodeTestFragment());
                } else {
                    showMsg("该设备暂不支持二维码功能测试");
                }
                break;
            }
            case R.id.mobile_network_test_button: {
                // 移动网络测试
                if (SystemUtils.isRk3399() && HardwareUtils.is20Devices() ||
                        SystemUtils.is4GFirmware() || SystemUtils.isYY4GFirmware()) {
                    showTitle(R.string.mobile_network_test);
                    activityBaseFragmentManager.replace(new MobileNetworkTestFragment());
                } else {
                    showMsg("该设备暂不支持移动网络功能测试");
                }
                break;
            }
            case R.id.finger_test_button: {              //指纹测试
                if (SystemUtils.isSupportZazFingerDevice()) {
                    if (!mMainActivity.checkIsTcDoorApp()) {        //通过太川门禁Aoo启动的自检程序不允许使用指纹测试功能，防止门禁程序与自检程序发视冲突
                        showTitle(R.string.finger_test);
                        activityBaseFragmentManager.replace(new TestFingerFragment());
                    } else {
                        showMsg("自检程序由第三方App启动，为避免功能冲突，不允许使用该功能");
                    }
                } else {
                    showMsg("该设备暂不支持指纹测试");
                }
                break;
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()) {
            case Key.Convert.KEYCODE_1:        //按键1——网络测试
                testNetworkBtn.performClick();
                break;
            case Key.Convert.KEYCODE_2:        //按键2——ADB测试
                adb_test_button.performClick();
                break;
            case Key.Convert.KEYCODE_3:        //按键3——视频测试
                video_test_button.performClick();
                break;
            case Key.Convert.KEYCODE_4:        //按键4——温度测试
                if (SystemUtils.isRk3399()) {
                    temp_test_button.performClick();
                } else {
                    showMsg("该设备暂无支持温度测试");
                }
                break;
            case Key.Convert.KEYCODE_5:             //按键5—— 双目摄像头测试
                doubleCameraTestBtn.performClick();
                break;
            case Key.Convert.KEYCODE_6:             //按键6—— 蓝牙功能测试
                btnBleTest.performClick();
                break;
            case Key.Convert.KEYCODE_7:
                // 按键7 二维码
                qrcodeTestBtn.performClick();
                break;
            case Key.Convert.KEYCODE_8:
                // 按键8 移动网络
                mobileNetworkBtn.performClick();
                break;
            case Key.Convert.KEYCODE_9:
                // 按键9 指纹测试
                fingerTestBtn.performClick();
                break;
            case Key.Convert.KEYCODE_JING:     //按键# ——测试报告
                check_result_button.performClick();
                break;
            case Key.Convert.KEYCODE_LONG_JING:    //长按# ——自动测试
                EventBus.getDefault().post(new TestResultEntity(0, getResources().getString(R.string.auto_check), PropertyDefaultValue.FUNCTION_TEST_RESULT));
                break;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        activityBaseFragmentManager = getActivityBaseFragmentManager();

        int doubleCameraTextId;
        if (!SystemUtils.isVankeSiteBoxDevice()){           //常规设备
            doubleCameraTextId = R.string.double_camera_test;
        }else {                                             //万科智慧工地项目设备
            doubleCameraTextId = R.string.third_camera_test;
        }
        doubleCameraTestBtn.setText(getStrById(doubleCameraTextId));
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {

    }

    /**
     * 定义一个方法用于 显示标题
     */
    private void showTitle(int title) {
        if (mMainActivity != null) {
//            if (mMainActivity.isStartAppByThree()){
//
//                mMainActivity.showToolBarTitle(title);
//            }
            mMainActivity.showToolBarTitle(title);
        }
    }

}
